package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.*;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class ExportServiceImpl implements ExportService {

    @Autowired
    private ExportDao exportDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ExportProductDao exportProductDao;
    @Autowired
    private ExtEproductDao extEproductDao;
    @Override
    public Export findById(String id) {
        return exportDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(Export export) {

        String contractIds = export.getContractIds();  //"fbf9409c-ce9b-4512-86cc-a665aeec2186,a2d5f5d4-59a9-4e70-bd76-a30f142afd9b"
//         货物的来源是从购销合同货物表中获取 select * from co_contract_product where contact_id in (ht001,ht002)
        ContractProductExample contractProductExample = new ContractProductExample();
        String[] contractIdArray = contractIds.split(",");
//        把数组转成集合
        List<String> contractIdList = Arrays.asList(contractIdArray);
        contractProductExample.createCriteria().andContractIdIn(contractIdList);
        List<ContractProduct> contractProductList = contractProductDao.selectByExample(contractProductExample);
//        有多少个ContractProduct就应该产生多少个ExportProduct
        for (ContractProduct contractProduct : contractProductList) {
            ExportProduct exportProduct = new ExportProduct();
//            给exportProduct赋值，对应的数据都是从contractProduct里面来的
//          BeanUtils是spring包下的    源对象   目标对象
            BeanUtils.copyProperties(contractProduct,exportProduct);
//            需要手动设置报运单附件的一个属性：exportId
            exportProduct.setExportId(export.getId());
            exportProductDao.insertSelective(exportProduct);


//            根据货物直接获取附件  因为货物和附件的一对多关系已经在映射文件中表示了
            List<ExtCproduct> extCproducts = contractProduct.getExtCproducts();
//            有多少个ExtCproduct就应该产生多少个ExtEproduct
            for (ExtCproduct extCproduct : extCproducts) {
                ExtEproduct extEproduct = new ExtEproduct();
//                给extEproduct赋值，对应的数据都是从extCproduct里面来的
                BeanUtils.copyProperties(extCproduct,extEproduct);
//                需要手动设置报运单附件的两个属性：exportProductId和exportId
                extEproduct.setExportProductId(exportProduct.getId());
                extEproduct.setExportId(export.getId());
//                需要手动设置报运单附件的两个属性：exportProductId和exportId
                extEproductDao.insertSelective(extEproduct);
            }
        }

//        在保存报运单时需要处理的数据有：货物的数量 附件的数量 设计到的购销合同的合同号（用空格隔开）

//        查询购销合同  select * from co_contract where id in (ht001,ht002)

        ContractExample example = new ContractExample();
        example.createCriteria().andIdIn(contractIdList);
        List<Contract> contractList = contractDao.selectByExample(example);

        int totalProNum=0;
        int totalExtNum=0;
        StringBuffer stringBuffer = new StringBuffer("");
        for (Contract contract : contractList) {
            totalProNum+=contract.getProNum();
            totalExtNum+=contract.getExtNum();
            stringBuffer.append(contract.getContractNo()).append(" ");
        }
        export.setProNum(totalProNum); // 所选择的购销合同的货物总数
        export.setExtNum(totalExtNum); // 所选择的购销合同的附件总数
        export.setCustomerContract(stringBuffer.toString());
        exportDao.insertSelective(export);
    }

    @Override
    public void update(Export export) {
        exportDao.updateByPrimaryKeySelective(export);
        List<ExportProduct> exportProducts = export.getExportProducts();
        if (exportProducts!=null&&exportProducts.size()>0){
            for (ExportProduct exportProduct : exportProducts) {
                exportProductDao.updateByPrimaryKeySelective(exportProduct);
            }
        }
    }

    @Override
    public void delete(String id) {
        exportDao.deleteByPrimaryKey(id);
    }

    @Override
    public PageInfo findAll(ExportExample example, int page, int size) {
        PageHelper.startPage(page,size);
        List<Export> list = exportDao.selectByExample(example);
        return new PageInfo(list);
    }
}
